//公用红包弹框
var app = getApp();
var server = require('../utils/server');
var common = require('../utils/common');
var voucherIds = [];
Component({
  /**
   * 组件的属性列表
   */
  externalClasses: ['member-class', 'member-close-class'],
  properties: {
    url: {
      type: String
    },
    scene: {
      type: String
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    showRedPackets: false,
    showRedPacketsOpen: false,
    showRedPacketsList: false,
    hbLists: [],
    pic_url: "",
    title: "",
    title1: "",
    coupon_params: "",
    timestap: Date.parse(new Date())
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showRedPackets() {
      server.postJSON(common.storePath + this.data.url, {
        "scene": this.data.scene,
        "city": app.globalData.city
      }, res => {
        if (res.data.errno == '0') {
          var data = res.data.data;
          this.setData({
            pic_url: data.pic_url,
            title: data.title,
            title1: data.title2
          })
          if (data.voucher_list.length > 0) {
            var new_voucher_list = [];
            for (var i = 0; i < data.voucher_list.length; i++) {
              data.voucher_list[i].isCanGet = true;
              new_voucher_list.push(data.voucher_list[i]);
              voucherIds.push(data.voucher_list[i].voucher_id)
            }
            this.setData({
              showRedPacketsOpen: true,
              showRedPackets: true,
              hbLists: new_voucher_list,
              coupon_params: data.coupon_params,
              send_coupon_params: data.coupon_params==""?'':data.coupon_params.send_coupon_params,
              sign: data.coupon_params==""?'':data.coupon_params.sign,
              send_coupon_merchant: data.coupon_params==""?'':data.coupon_params.send_coupon_merchant
            })
          } else {
            this.setData({
              showRedPacketsOpen: false,
              showRedPackets: false
            })
          }
        } else {
          this.setData({
            showRedPackets: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    },
    openPacketsList(e) {
      var curtarget = e.currentTarget.dataset;
      let voucherid = curtarget.voucherid;
      this.setData({
        ['hbLists[0].isCanGet']: false
      })
      var param = {};
      console.log(this.data.hbLists)
      if (this.data.hbLists.length > 1) {
        param = {
          "voucher_id": "",
          "amount": "",
          "is_new": "",
          "city": app.globalData.city,
          "voucher_list": voucherIds
        }
      } else {
        param = {
          "voucher_id": voucherid,
          "amount": "",
          "is_new": "",
          "city": app.globalData.city
        }
      }
      server.postJSON(common.storePath + 'voucher/qzsendcoupon/', param, res => {
        if (res.data.errno == '0') {
          server.postJSON(common.basePath + 'user/isshow', {
            "type": "znq",
            "is_checked": 1
          }, () => {
            console.log('closepop')
          })
          this.setData({
            showRedPacketsOpen: false,
            showRedPacketsList: true
          })
          this.setData({
            ['hbLists[0].is_has']: 1,
            ['hbLists[0].isCanGet']: true
          })
        } else {
          this.setData({
            ['hbLists[0].isCanGet']: true,
            showRedPackets: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    },
    //微信商家券
    getcoupon(params) {
      console.log(params)
      var curtarget = params.currentTarget.dataset;
      let voucherid = curtarget.voucherid;
      this.setData({
        ['hbLists[0].isCanGet']: false
      })
      var param = {
        "voucher_id": voucherid,
        "city": app.globalData.city,
        "amount": "0",
        "is_new": '',
        "voucher_list": [],
        "send_coupon_result": params.detail.send_coupon_result
      }
      if (params.detail.send_coupon_result[0].code == 'SUCCESS') {
        server.postJSON(common.storePath + 'voucher/qzsendcoupon/', param, res => {
          if (res.data.errno == '0') {
            server.postJSON(common.basePath + 'user/isshow', {
              "type": "znq",
              "is_checked": 1
            }, () => {
              console.log('closepop')
            })
            this.setData({
              showRedPacketsOpen: false,
              showRedPacketsList: true
            })
            this.setData({
              ['hbLists[0].is_has']: 1,
              ['hbLists[0].isCanGet']: true
            })
          } else {
            this.setData({
              ['hbLists[0].isCanGet']: true,
              showRedPackets: false
            })
            common.showErrorToast(res.data.errmsg);
          }
        })
      } else {
        this.setData({
          showRedPackets: false
        })
        common.showErrorToast(params.detail.send_coupon_result[0].message);
      }
    },
    hideRedPackets() {
      this.setData({
        showRedPackets: false
      })
    },
    goToProductIndex() {
      this.triggerEvent('changepage')
      this.setData({
        showRedPackets: false
      })
    }
  }
})